<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Common\Controller\AuthController;

Class ConfigController extends AuthController {
	protected $config_Classification = 'config_classification'; //配置分类表名
	protected $config_table_name = 'config'; //配置表名
	protected $config_path = 'Application/Common/Conf/common.php'; //配置文件路径
	//配置管理
	public function index() {
		if (IS_POST) {
			$this->error('非法操作!');

		} else {

			$config = M($this->config_table_name)->field(true)->order('sort ASC')->select();
			$config_Class = M($this->config_Classification)->field(true)->order('sort ASC')->select();
			for ($i = 0; $i < count($config); $i++) {
				$config[$i]['update_time'] = friendlyDate($config[$i]['update_time'], 'normal'); //更新时间
				$config[$i]['create_time'] = friendlyDate($config[$i]['create_time'], 'normal'); //创建时间
				$config[$i]['group'] = ChangeData($config_Class, $config[$i]['group'], 'id');
			}
			//配置分类按组排序
			for ($k = 0; $k < count($config_Class); $k++) {
				$config_Class[$k]['child'] = $this->list_group($config, $config_Class[$k]['id']);
			}
			$this->assign('group', $config_Class);
			$list['config'] = $config_Class;

			$this->assign('list', $list);
			//添置操作按钮
			$array[] = array('title' => '新增', 'url' => U('addConfig'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
			$array[] = array('title' => '一键生成', 'url' => U('create_config'), 'class' => 'danger');
			$this->assign('add_button', add_button($array));
			$this->display();
		}
	}
	//配置权限按组排序
	private function list_group($config, $init) {
		for ($i = 0; $i < count($config); $i++) {
			if ($config[$i]['group']['id'] == $init) {
				$keys[] = $config[$i];
			}
		}
		return $keys;
	}
	//新增配置
	public function addConfig() {
		if (IS_POST) {
			$_POST['create_time'] = time();
			$_POST['update_time'] = time();

			if (M($this->config_table_name)->add($_POST)) {
				$this->success('新增配置添加成功!', U('Admin/Config/index'));
			} else {
				$this->error('新增失败!');
			}
		}
	}
	//删除配置项
	public function delete_Config() {
		$map['id'] = I('get.id');
		if (M($this->config_table_name)->where($map)->delete()) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}

	}
	//一键生成配置
	public function create_config() {
		$config = M($this->config_table_name)->field('name,value')->select();

		//处理成一维数组
		foreach ($config as $key => $value) {
			$path_array[$config[$key]['name']] = $config[$key]['value'];
		}
		//写入文件
		if ($this->create_array_dir($this->config_path, $path_array)) {
			$this->success('更新配置成功!', U('Admin/Config/index'));
		} else {
			$this->error($this->config_path . '不可写!');
		}

	}
	//修改配置项
	public function saeConfig() {
		if (IS_POST) {
			$_POST['update_time'] = time();
			$map['id'] = I('post.id');
			unset($_POST['id']);
			if (M($this->config_table_name)->where($map)->save($_POST)) {
				$this->success('操作成功!', U('Admin/Config/index'));
			} else {
				$this->error('操作失败!');
			}
		} else {
			$set = M($this->config_Classification)->field(true)->where(array('auth' => 1))->select();
			$config = M($this->config_table_name)->where(array('id' => I('get.id')))->find();
			// 默认选中状态设置
			$ifaction = array_where_select($set, 'id', $config['group']);
			$this->assign('ifaction', $ifaction);
			$this->assign('config', $config);

			$this->display();
		}
	}
	//增配置分类
	public function addClassification() {
		if (IS_POST) {
			$data['name'] = I('post.name');
			$config = M($this->config_Classification)->add($data);
			if ($config) {
				$this->success('操作成功!', 'Classification');
			} else {
				$this->error('操作失败!');
			}
		}
	}
	//配置分类
	public function Classification() {
		if (IS_POST) {
			$this->error('非法操作!');
		} else {
			$config = M($this->config_Classification)->field(true)->order('sort ASC')->select();
			$this->assign('Classification', $config);

			//添置操作按钮
			$array[] = array('title' => '新增分类', 'url' => U('addClassification'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
			$this->assign('add_button', add_button($array));
			$this->display();
		}

	}
	//配置分类修改
	public function romClassification() {
		$name = I('get.name');
		$val = I('get.val');
		$id = I('get.id');
		$data[$name] = $val;
		if (M($this->config_Classification)->where(array('id' => $id))->save($data)) {
			$json['status'] = true;
			$json['content'] = "成功修改为{$val}";
		} else {
			$json['status'] = false;
			$json['content'] = "修改失败!";
		}
		$this->ajaxReturn($json);

	}
	//删除配置分类
	public function delete_Classification() {
		if (!I('get.id')) {
			$this->error('非法操作');
		}
		$return = M($this->config_Classification)->delete(I('get.id'));
		if ($return) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}
	//ajax验证配置标识是否存在
	public function config_name() {
		if (IS_POST) {
			$name = I('post.name');
			$param = I('post.param');
			$getid = I('get.id');
			//如果GET['id']存在，返回false即为正确信息
			if ($getid) {
				$map['id'] = array('NEQ', $getid);
				$return = D($this->config_table_name)->where($map)->where(array($name => $param))->field(true)->find();
			} else {
				$return = D($this->config_table_name)->where(array($name => $param))->field(true)->find();
			}

			if ($return) {
				$data['info'] = I('post.param') . '已存在!';
				$data['status'] = 'n';
			} else {
				$data['info'] = '信息填写正确!';
				$data['status'] = 'y';
			}
			$this->ajaxReturn($data);
		}
	}
	//将数组写入文件
	private function create_array_dir($path, $array) {
		if (is_writable($path)) {
			$of = fopen($path, 'w'); //创建并打开dir.txt
			if ($of) {
				fwrite($of, '<?php  return ');
				fwrite($of, var_export($array, true)); //把执行文件的结果写入文件
				fwrite($of, ';');
			}
			fclose($of); //关闭
			return true;
		} else {
			return false;
		}
	}

	//插件列表
	public function Hook() {

		$number = 38; //模型id
		//添置按钮
		$array[] = array('title' => '添加插件', 'url' => U('addHook'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
		$this->assign('add_button', add_button($array));
		$this->assign('FormConfig', array('title' => '添加费用', 'url' => U('addHook', array('mid' => $number))));
		//获取模型表单数据
		$this->FormHtml($number);
		$type[] = array('title' => '修改', 'url' => 'romHook', 'class' => 'default', 'type' => 'show');
		$type[] = array('title' => '调用', 'url' => 'showHook', 'class' => 'default', 'type' => 'delete');
		$this->assign('array', $type);
		$this->display('Public/_List');
	}
	//添加插件
	public function addHook() {
		if (IS_POST) {
			//保存询盘信息
			$chajianmulu = I('post.chajianmulu');

			$this->PublicDataModel(I('get.mid'));
		}
	}
	//修改
	public function romHook() {
		$mid = I('get.mid', '', 'intval');
		$id = I('get.id', '', 'intval');
		if (IS_POST) {
			$this->PublicDataModel($mid, $id, U('index'));
		} else {
			$this->PublicDataModel($mid, $id);
		}
	}
	public function showHook() {
		$id = I('get.id', '', 'intval');
		$this->error("模板调用方法：{:hook($id,array())}");
	}
}